/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Base.ImageTest;
import Base.Perguntas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import net.java.balloontip.BalloonTip;
import net.java.balloontip.CustomBalloonTip;
import net.java.balloontip.styles.EdgedBalloonStyle;

/**
 *
 * @author Guilherme
 */
public class Pergunta extends javax.swing.JFrame {

  
    public Pergunta() {
        if(!Base.Perguntas.apresentar){
            this.pergunta = Base.Perguntas.proxima();
            Base.Perguntas.init = System.currentTimeMillis();
        }else{
            this.pergunta = Base.Perguntas.atual();
        }
        
        initComponents();
        lblPosicao.setText(Base.Perguntas.qtde());
        resposta = -1;
        icon = new ImageIcon();
        setarImagem();
        if(Base.Perguntas.apresentar){
            lblVoltar.setVisible(false);
            setarIcones();
        }
    }
    
    private void setarIcones(){
        ImageIcon iconA=new ImageIcon();
        ImageIcon iconB=new ImageIcon();
        ImageIcon iconC=new ImageIcon();
        ImageIcon iconD=new ImageIcon();
        
        try {  
            iconA.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_a_verde.png")));  
            iconB.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_b_verde.png")));  
            iconC.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_c_verde.png")));  
            iconD.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_d_verde.png"))); 
            resposta = Base.Perguntas.respostaAluno;
            
            switch(resposta){
                case 0:
                    iconA.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_a_cinza.png")));  
                    break;
                case 1:
                    iconB.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_b_cinza.png")));  
                    break;
                case 2:
                    iconC.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_c_cinza.png")));  
                    break;
                case 3:
                    iconD.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_d_cinza.png")));  
                    break;
            }
            
        }catch(IOException ex) {
            
            Logger.getLogger(Adm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        lblA.setIcon(iconA);
        lblB.setIcon(iconB);
        lblC.setIcon(iconC);
        lblD.setIcon(iconD);
        lblA.repaint();
        lblB.repaint();
        lblC.repaint();
        lblD.repaint();
               
    }
    
     
     public String preencherBalao(){
         String retorno = "";         
         retorno = pergunta.getHtml();
         return retorno;
     }
     
     private void setarImagem(){
         if(pergunta.getImagem()!=null){
             ImageTest.writeByteArrayToFile(pergunta.getImagem());
             caminho = ".\\src\\Imagens\\busca.png";
             addImagem();
         }else{
             lblImagem.setVisible(false);
         }
     }
     
     private void addImagem(){
        lblImagem.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblImagem.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
        BufferedImage original = null;
        try {
            original = ImageIO.read(new File(caminho));
        } catch (IOException ex) {
            Logger.getLogger(Adm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        int origW = 0;
        int origH = 0;
        float fator = 0;
                
        if (original.getWidth() >= original.getHeight()) {  
            fator = ((float) (lblImagem.getWidth())) / ((float) (original.getWidth()));
        } else {             
            fator = ((float) lblImagem.getHeight()) / ((float) original.getHeight());            
        }
        
        origW = (int) (original.getWidth() * fator);
        origH = (int) (original.getHeight() * fator);
       
        if (origW > lblImagem.getWidth()) {
            origW = lblImagem.getWidth();
        }
        if (origH > lblImagem.getHeight()) {
            origH = lblImagem.getHeight();
        }

        if (original.getWidth() <= lblImagem.getWidth() && original.getHeight() <= lblImagem.getHeight()) {
            origW = original.getWidth();
            origH = original.getHeight();
        }
       
        ImageTest.exec(caminho, origW, origH);
        try {
            icon.setImage(ImageIO.read(new File(".\\src\\Imagens\\result.png")));
        } catch (IOException ex) {
            Logger.getLogger(Adm.class.getName()).log(Level.SEVERE, null, ex);
        }
        lblImagem.repaint();
        lblImagem.setIcon(icon);
        lblImagem.repaint();
    }
     
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPergunta = new javax.swing.JPanel();
        lblProfessor = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblImagem = new javax.swing.JLabel();
        lblB = new javax.swing.JLabel();
        lblC = new javax.swing.JLabel();
        lblD = new javax.swing.JLabel();
        lblA = new javax.swing.JLabel();
        lblVoltar = new javax.swing.JLabel();
        lblAvancar = new javax.swing.JLabel();
        lblPosicao = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Quiz Responda e Aprenda");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        getContentPane().setLayout(null);

        pnlPergunta.setBackground(new java.awt.Color(253, 199, 111));
        pnlPergunta.setPreferredSize(new java.awt.Dimension(920, 583));
        pnlPergunta.setLayout(null);

        JLabel ins = new JLabel(preencherBalao());
        ins.setFont(new java.awt.Font("Tahoma", 0, 14));
        final Rectangle customOffset = new Rectangle(10, 10, 10, 10);
        lblProfessor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/professor2.png"))); // NOI18N
        CustomBalloonTip balloonTip = new CustomBalloonTip(lblProfessor, ins, customOffset,
            new EdgedBalloonStyle(Color.WHITE, Color.BLUE),
            BalloonTip.Orientation.LEFT_ABOVE,
            BalloonTip.AttachLocation.NORTHEAST,
            10, 10, false);
        balloonTip.setPadding(5);
        balloonTip.setVisible(true);
        //                balloonTip.setBounds(10, 10, 100, 50);
        pnlPergunta.add(balloonTip);
        pnlPergunta.add(lblProfessor);
        lblProfessor.setBounds(30, 230, 200, 320);
        pnlPergunta.add(jLabel3);
        jLabel3.setBounds(440, 460, 0, 0);
        pnlPergunta.add(jLabel4);
        jLabel4.setBounds(560, 460, 0, 0);

        jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel5MouseClicked(evt);
            }
        });
        pnlPergunta.add(jLabel5);
        jLabel5.setBounds(370, 460, 0, 0);
        pnlPergunta.add(jLabel6);
        jLabel6.setBounds(500, 460, 0, 0);

        lblImagem.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        lblImagem.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlPergunta.add(lblImagem);
        lblImagem.setBounds(280, 260, 380, 180);

        lblB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alternativas/alternativa_b_verde.png"))); // NOI18N
        lblB.setToolTipText("Alternativa B");
        lblB.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblBMouseClicked(evt);
            }
        });
        pnlPergunta.add(lblB);
        lblB.setBounds(410, 470, 40, 30);

        lblC.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alternativas/alternativa_c_verde.png"))); // NOI18N
        lblC.setToolTipText("Alternativa C");
        lblC.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblCMouseClicked(evt);
            }
        });
        pnlPergunta.add(lblC);
        lblC.setBounds(510, 470, 40, 30);

        lblD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alternativas/alternativa_d_verde.png"))); // NOI18N
        lblD.setToolTipText("Alternativa D");
        lblD.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblD.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblDMouseClicked(evt);
            }
        });
        pnlPergunta.add(lblD);
        lblD.setBounds(600, 470, 40, 30);

        lblA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alternativas/alternativa_a_verde.png"))); // NOI18N
        lblA.setToolTipText("Alternativa A");
        lblA.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAMouseClicked(evt);
            }
        });
        pnlPergunta.add(lblA);
        lblA.setBounds(310, 470, 40, 30);

        lblVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/ant2.png"))); // NOI18N
        lblVoltar.setToolTipText("Voltar");
        lblVoltar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblVoltar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblVoltarMouseClicked(evt);
            }
        });
        pnlPergunta.add(lblVoltar);
        lblVoltar.setBounds(720, 490, 80, 50);

        lblAvancar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/prox2.png"))); // NOI18N
        lblAvancar.setToolTipText("Avançar");
        lblAvancar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblAvancar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAvancarMouseClicked(evt);
            }
        });
        pnlPergunta.add(lblAvancar);
        lblAvancar.setBounds(810, 490, 80, 50);

        lblPosicao.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblPosicao.setText("1/10");
        pnlPergunta.add(lblPosicao);
        lblPosicao.setBounds(840, 20, 50, 20);

        getContentPane().add(pnlPergunta);
        pnlPergunta.setBounds(-10, -10, 930, 580);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-920)/2, (screenSize.height-602)/2, 920, 602);
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel5MouseClicked
        
    }//GEN-LAST:event_jLabel5MouseClicked

    private void lblAvancarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAvancarMouseClicked
        
        if(resposta != -1){
            Perguntas.acertou(resposta);
            Base.Perguntas.apresentar=false;
            Base.Perguntas.fin = System.currentTimeMillis();
            Base.Perguntas.calcular();
            Resposta r = new Resposta();  
            this.setVisible(false); 
            r.setVisible(true);                       
        }else{            
            Base.Perguntas.ultima++;
            Base.Perguntas.voltar = false;  
            this.setVisible(false);
            Pergunta p = new Pergunta(); 
            p.setVisible(true);            
        }
        
    }//GEN-LAST:event_lblAvancarMouseClicked

    private void lblAMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAMouseClicked
        if(Base.Perguntas.apresentar){
            return;
        }
        ImageIcon iconA=new ImageIcon();
        ImageIcon iconB=new ImageIcon();
        ImageIcon iconC=new ImageIcon();
        ImageIcon iconD=new ImageIcon();
        
        try {  
            iconA.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_a_cinza.png")));  
            iconB.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_b_verde.png")));  
            iconC.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_c_verde.png")));  
            iconD.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_d_verde.png")));  
        }catch(IOException ex) {
            
            Logger.getLogger(Adm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        lblA.setIcon(iconA);
        lblB.setIcon(iconB);
        lblC.setIcon(iconC);
        lblD.setIcon(iconD);
        lblA.repaint();
        lblB.repaint();
        lblC.repaint();
        lblD.repaint();
        resposta = 0;        
    }//GEN-LAST:event_lblAMouseClicked

    private void lblBMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblBMouseClicked
        if(Base.Perguntas.apresentar){
            return;
        }
        ImageIcon iconA=new ImageIcon();
        ImageIcon iconB=new ImageIcon();
        ImageIcon iconC=new ImageIcon();
        ImageIcon iconD=new ImageIcon();
        
        try {  
            iconA.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_a_verde.png")));  
            iconB.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_b_cinza.png")));  
            iconC.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_c_verde.png")));  
            iconD.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_d_verde.png")));  
        }catch(IOException ex) {
            
            Logger.getLogger(Adm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        lblA.setIcon(iconA);
        lblB.setIcon(iconB);
        lblC.setIcon(iconC);
        lblD.setIcon(iconD);
        lblA.repaint();
        lblB.repaint();
        lblC.repaint();
        lblD.repaint();
        resposta = 1;        
    }//GEN-LAST:event_lblBMouseClicked

    private void lblCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblCMouseClicked
        if(Base.Perguntas.apresentar){
            return;
        }
        
        ImageIcon iconA=new ImageIcon();
        ImageIcon iconB=new ImageIcon();
        ImageIcon iconC=new ImageIcon();
        ImageIcon iconD=new ImageIcon();
        
        try {  
            iconA.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_a_verde.png")));  
            iconB.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_b_verde.png")));  
            iconC.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_c_cinza.png")));  
            iconD.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_d_verde.png")));  
        }catch(IOException ex) {
            
            Logger.getLogger(Adm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        lblA.setIcon(iconA);
        lblB.setIcon(iconB);
        lblC.setIcon(iconC);
        lblD.setIcon(iconD);
        lblA.repaint();
        lblB.repaint();
        lblC.repaint();
        lblD.repaint();
        resposta = 2;
    }//GEN-LAST:event_lblCMouseClicked

    private void lblDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblDMouseClicked
        if(Base.Perguntas.apresentar){
            return;
        }
        
        ImageIcon iconA=new ImageIcon();
        ImageIcon iconB=new ImageIcon();
        ImageIcon iconC=new ImageIcon();
        ImageIcon iconD=new ImageIcon();
        
        try {  
            iconA.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_a_verde.png")));  
            iconB.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_b_verde.png")));  
            iconC.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_c_verde.png")));  
            iconD.setImage(ImageIO.read(new File(".\\src\\Imagens\\alternativas\\alternativa_d_cinza.png")));  
        }catch(IOException ex) {
            
            Logger.getLogger(Adm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        lblA.setIcon(iconA);
        lblB.setIcon(iconB);
        lblC.setIcon(iconC);
        lblD.setIcon(iconD);
        lblA.repaint();
        lblB.repaint();
        lblC.repaint();
        lblD.repaint();
        resposta = 3;
    }//GEN-LAST:event_lblDMouseClicked

    private void lblVoltarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblVoltarMouseClicked
        Base.Perguntas.voltar = true;
        Pergunta p = new Pergunta();
        this.setVisible(false);
        p.setVisible(true);        
    }//GEN-LAST:event_lblVoltarMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Pergunta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Pergunta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Pergunta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Pergunta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Pergunta().setVisible(true);
            }
        });
    }
    
    private String caminho;
    private ImageIcon icon;
    private int resposta;
    private Base.Pergunta pergunta;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel lblA;
    private javax.swing.JLabel lblAvancar;
    private javax.swing.JLabel lblB;
    private javax.swing.JLabel lblC;
    private javax.swing.JLabel lblD;
    private javax.swing.JLabel lblImagem;
    private javax.swing.JLabel lblPosicao;
    private javax.swing.JLabel lblProfessor;
    private javax.swing.JLabel lblVoltar;
    private javax.swing.JPanel pnlPergunta;
    // End of variables declaration//GEN-END:variables
}
